#!/bin/bash
 
#-------------------------------------------------------------------------------
# isccommand.sh
# 
# This shell script is invoked from the Inventory Scout data collection tasks
# for the process of copying data from the hard drive to diskette.
#
# Currently all we'll do is copy the data, but 1) we might need to span
# diskettes, and 2) might consider compressing (zip) the data to conserve
# space (which might require multiple diskettes as well).
# 
# Usage: isccommand <input file to copy> <output file>
#
# Return Codes:
# 1 - Error accessing required save update data logging directory
# 2 - Media is write-protected
# 3 - Error copying data to removable media
# 4 - Error mounting the media
# 5 - input file does not exist
# 6 - Error unmounting the media
# 7 - Generic other error(?)

#---------------------------------------------------------------------------------
# directory and filename which records the Invemntory Scout commands
#---------------------------------------------------------------------------------
LOGDIR=/var/hsc/log
LOG=$LOGDIR/isccommand.log
   
LOG_ERROR_LOG=/tmp/isccommand.log

#-------------------------------------------------------------------------------
# the filename of the list of files to be saved for this subsystem.
#-------------------------------------------------------------------------------
INPUT_FILE=$1

#-------------------------------------------------------------------------------
# the output filename of the save upgrade data archive (zip file)
#-------------------------------------------------------------------------------
OUTPUT_FILE=$2

#-------------------------------------------------------------------------------
# the mount point for the media to which Invemntory Scout command copies either
# the microcode survey ar VPD information to
#-------------------------------------------------------------------------------
MOUNTPOINT=/mnt/floppy

# Check if the directory for the log file exists.
if [ ! -d $LOGDIR ]; then
	echo "=================================================================" > $LOG_ERROR_LOG
	echo -e "Inventory Scout command log for `date`." >> $LOG_ERROR_LOG
	echo "Inventory Scout command log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
	exit 1
fi

# Start log to record the Inventory Scout command actions.
echo -e "Inventory Scout Data collection log for `date`.\n" > $LOG
echo -e "parm 1 = $INPUT_FILE, parm 2 = $OUTPUT_FILE" >> $LOG


# Check to see if media is already mounted - if so then unmount it
if grep "$MOUNTPOINT" /etc/mtab; then # Media is already mounted
	if ! umount $MOUNTPOINT >> $LOG 2>&1; then
		echo "Couldn't unmount the media, $MOUNTPOINT." >> $LOG
		exit 6
	fi
fi

# Mount the media and check for write-protect
if mount -v $MOUNTPOINT > mount_output 2>&1; then
	echo "Media successfully mounted at $MOUNTPOINT." >> $LOG
	if grep "write-protected" mount_output; then
		echo "The media is write-protected." >> $LOG
		umount $MOUNTPOINT
		exit 2
	fi
else
	echo "Couldn't mount the media, $MOUNTPOINT." >> $LOG
	exit 4
fi


# Double check one to see if input file really exists
if [ ! -e $INPUT_FILE ]; then
        echo "File, $INPUT_FILE, does not exist on the HSC." >> $LOG
        umount $MOUNTPOINT
        exit 5
fi


# Need to check for diskspace problems or other errors and
# perhaps add possible error recovery message.
echo "Copying Inventory Scout data file named $INPUT_FILE to $MOUNTPOINT/$OUTPUT_FILE." >> $LOG

#
# Save to floppy
#
# copy the data to floppy - assume it's been formatted
cp -f -v $INPUT_FILE $MOUNTPOINT/$OUTPUT_FILE
if [ $? -ne 0 ] ; then
        echo "Error $? occurred while copying the Inventory Scout data to floppy." >> $LOG
        umount $MOUNTPOINT
        exit 3
fi

# bit-o-status
echo "Data successfully copied to diskette." >> $LOG


# Unmount the target media and log completion
if umount -v $MOUNTPOINT >> $LOG 2>&1; then
	echo "Successful completion." >> $LOG
	exit 0
else
        # yeah, this is an unexpected error, but the data on diskette is still valid...
        # should we return a "success" then?
	echo "Couldn't unmount the media at mount point $MOUNTPOINT." >> $LOG
	exit 6
fi
